package com.phonebills.dao.impl;

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.phonebills.dao.BaseMapper;
import com.phonebills.dao.UserDao;
import com.phonebills.vo.Bills;
import com.phonebills.vo.Page;
import com.phonebills.vo.User;

/**
 * 用户数据访问实现
 * @author LIU
 * @version 1.0 2017-10-27
 */
public class UserDaoImpl extends BaseMapper implements UserDao {

	String path = "mybatis.xml";
	
	/**
	 * 用户登陆
	 *TODO
	 *LIU
	 * @param user
	 * @return boolean
	 *下午4:03:47
	 */
	@Override
	public int login(User user) {
		// TODO Auto-generated method stub
		int result = 0;
		SqlSession session = null;
		try {
			session = getSession(path);
			result = session.selectOne("com.phonebills.dao.UserDao.login", user);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			session.close();
		}
		return result;
	}

	
	/**
	 * 获取所有账单的总记录数
	 *TODO
	 *LIU
	 * @param page
	 * @return
	 *下午4:16:37
	 */
	@Override
	public int getAllCounts() {
		// TODO Auto-generated method stub
		int result = 0;
		SqlSession session = null;
		try {
			session = getSession(path);
			result = session.selectOne("com.phonebills.dao.UserDao.getAllCounts");
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			session.close();
		}
		return result;
	}


	
	/**
	 * 分页显示所有账单
	 *TODO
	 *LIU
	 * @param page
	 * @return
	 *下午4:20:11
	 */
	@Override
	public List<Bills> showAll(Page page) {
		// TODO Auto-generated method stub
		List<Bills> billsList = null;
		SqlSession session = null;
		try {
			session = getSession(path);
			billsList = session.selectList("com.phonebills.dao.UserDao.showAll", page);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			session.close();
		}
		return billsList;
	}


	/**
	 * 获取当前用户
	 *TODO
	 *LIU
	 * @param no
	 * @return
	 *下午5:39:25
	 */
	@Override
	public User getCurrUser(String no) {
		// TODO Auto-generated method stub
		User user = null;
		SqlSession session = null;
		try {
			session = getSession(path);
			user = session.selectOne("com.phonebills.dao.UserDao.getCurrUser", no);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			session.close();
		}
		return user;
	}

}
